﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Linq;
using OfficeOpenXml;

namespace ToolExcelPub
{
    public static partial class XTool
    {
        public static void ReadEnum()
        {
            ShowInfo("ReadEnum!");

            var DictEnumTemp = new Dictionary<string, Dictionary<string, string>>();

            foreach (var fileinfo in DictXFileInfo.Values)
            {
                if (!fileinfo.HasType(EToolFileType.枚举))
                {
                    continue;
                }
                var PathEnum = fileinfo.FileName;
                if (File.Exists(PathEnum))
                {
                    using (var package = new ExcelPackage(new FileInfo(PathEnum)))
                    {
                        var wk = package.Workbook;
                        foreach (var sheet in wk.Worksheets)
                        {
                            var rows = sheet.Dimension.End.Row;
                            var enumcols = sheet.Dimension.End.Column;

                            for (int j = 1; (j + 2) <= enumcols; j += 3)
                            {
                                //枚举名
                                string DictKey = "";
                                var celldictk = sheet.GetValue(2, j);
                                if (celldictk != null)
                                {
                                    DictKey = celldictk.ToString();
                                }
                                if (string.IsNullOrEmpty(DictKey))
                                {
                                    break;
                                }
                                //枚举注释
                                string DictKeyDesc = "";
                                var celldictkd = sheet.GetValue(1, j);
                                if (celldictkd != null)
                                {
                                    DictKeyDesc = celldictkd.ToString();
                                }
                                if (string.IsNullOrEmpty(DictKey))
                                {
                                    DictKeyDesc = "";
                                }
                                Dictionary<string, string> DictList = new Dictionary<string, string>();
                                for (int i = 4; i <= rows; i++)
                                {
                                    var cell1 = sheet.GetValue(i, j);
                                    var cell2 = sheet.GetValue(i, j + 1);
                                    if (cell1 == null || cell2 == null)
                                    {
                                        continue;
                                    }
                                    string enumK = cell2.ToString();
                                    string enumV = cell1.ToString();
                                    if (enumK == "" || enumV == "")
                                    {
                                        continue;
                                    }
                                    if (DictList.ContainsKey(enumK))
                                    {
                                        ShowError($"重复的枚举表Key {DictKey} {enumK}", "提示");
                                        continue;
                                    }
                                    if (DictList.ContainsKey(enumV))
                                    {
                                        ShowError($"重复的枚举表Value {DictKey} {enumV}", "提示");
                                        continue;
                                    }
                                    DictList[enumK] = enumV;
                                }
                                if (DictEnumTemp.ContainsKey(DictKey))
                                {
                                    ShowError($"重复的枚举表类型 {DictKey}", "提示");
                                }
                                else
                                {
                                    DictEnumTemp[DictKey] = DictList;
                                    DictDictEnumPubDesc[DictKey] = DictKeyDesc;
                                }
                            }
                        }
                    }
                    
                }
            }
            

            DictDictEnumPub = (from it in DictEnumTemp orderby it.Key ascending select it).ToDictionary(it => it.Key, it => it.Value);
        }

        /// <summary>
        /// 通过字段名和值查询枚举中是否已经存在值
        /// </summary>
        /// <param name="headEnum"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static string GetEnumValue(string headEnum, string v)
        {
            string value = v;
            if (!string.IsNullOrEmpty(headEnum))
            {
                if (DictDictEnumPub.TryGetValue(headEnum, out var DictList1))
                {
                    if (DictList1.ContainsKey(value))
                    {
                        value = DictList1[value];
                    }
                }
                if (DictDictEnumPageId.TryGetValue(headEnum, out var DictList2))
                {
                    if (DictList2.ContainsKey(value))
                    {
                        value = DictList2[value];
                    }
                }
                if (DictDictEnumPage.TryGetValue(headEnum, out var DictList3))
                {
                    if (DictList3.ContainsKey(value))
                    {
                        value = DictList3[value];
                    }
                }
            }
            return value;
        }
    }
}
